{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "How to extract matching sub meshes from a common mesh.\n",
    "\"\"\"\n",
    "from dolfin import *\n",
    "\n",
    "class Structure(SubDomain):\n",
    "    def inside(self, x, on_boundary):\n",
    "        return x[0] > 1.4 - DOLFIN_EPS and x[0] < 1.6 \\\n",
    "            + DOLFIN_EPS and x[1] < 0.6 + DOLFIN_EPS\n",
    "\n",
    "mesh = RectangleMesh(Point(0.0, 0.0), Point(3.0, 1.0), 60, 20)\n",
    "\n",
    "# Create sub domain markers and mark everaything as 0\n",
    "sub_domains = MeshFunction(\"size_t\", mesh, mesh.topology().dim())\n",
    "sub_domains.set_all(0)\n",
    "\n",
    "# Mark structure domain as 1\n",
    "structure = Structure()\n",
    "structure.mark(sub_domains, 1)\n",
    "\n",
    "# Extract sub meshes\n",
    "fluid_mesh = SubMesh(mesh, sub_domains, 0)\n",
    "structure_mesh = SubMesh(mesh, sub_domains, 1)\n",
    "\n",
    "# Move structure mesh\n",
    "for x in structure_mesh.coordinates():\n",
    "    x[0] += 0.1*x[0]*x[1]\n",
    "\n",
    "# Move fluid mesh according to structure mesh\n",
    "ALE.move(fluid_mesh, structure_mesh)\n",
    "fluid_mesh.smooth()\n",
    "\n",
    "#############################################\n",
    "from vtkplotter.dolfin import plot\n",
    "\n",
    "plot(fluid_mesh)\n",
    "plot(structure_mesh, c='tomato', add=True)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
